<html>
<head><title>Dense Script documentation</title></head>
<body>
Content:<br>
<a href="#intro">1. Introduction</a><br>
<a href="#general">2. General syntax notes</a><br>
<a href="#datatypes">3. Data types</a><br>
<a href="#operators">4. Operators</a><br>
<a href="#expstat">5. Expression related statements.</a><br>
<a href="#ctrlflow">6. Control flow statements</a><br>
<a href="#func">7. Functions and methods.</a><br>
<a href="#io">8. Input and output</a><br>

<h3 id="intro">1. Introduction</h3>

Dense script is simple scripting language created on a whim
for a 1krl contest. General design rule is "compact script source".
But without "cheating" like C macroses. Parser of dense script is
one pass parser with one symbol look ahead and without backtrace.
This results in some limitations. Operators do not have precedence.
A op B op C is always parsed as (A op B) op C. Conditional expressions
cannot start with brace.

<h3 id="general">2. General syntax notes</h3>

Dense script program is a sequence of statements and functions and methods definitions. There are
control flow statements, expressions related statements and IO statements.
All variables, functions and methods are 1 symbol only.
Most "keywords" are also 1 symbol.<br>
Comments in dense script start with <b>#!</b>. There are no multiline comments.

<h3 id="datatypes">3. Data types</h3>

There are two basic types: int and string.
Two container types: array and structure.
Several "subset reference" types: array segment and slice, string segment and slice,
and struct slice.
Function and method reference types.
Expression alias type. And internal type - variable reference.
Variables are bound to the type of the first expression assigned to them.
Segment is a continuous subset of a string or an array.
Slice is an ordered subset of a string or array defined by array of indecies.
<br>Constructor of int type is obviously integer constant. <br>Constructor of string is double quotes.
Strings are interpolated. String "x=$x" is equivalent to "x="+x. Anything more complex than
single variable should surrounded in bracers - "x+y=$(x+y)".<br>
Constructor of array is curly bracers with values separated by comma. For example a={1,2,3}.<br>
Constructor of structure is also curly bracers with values separated by comma and prefixed
by field name followed by colon. For example s={x:1,y:2}<br>
Constructor of function or method reference is &. For example: r=&f.<br>
Constructor of expression alias is ^. For example d=^x*x. Expression alias is not text macro.
It's expression with delayed evaluation. Expression is evaluated in a local context.
There are no constructors for segments and slices. They appear as a result of several operations.
Constructor of struct slice is single quote. Structure to the left side and ordered set of fields to the right.
For example: a'xy is a slice of structure a with fields x and y in that specific order.

<h3 id="operators">4. Operators.</h3>

Depending on type of operands, result of various operations may vary.<br>

<b>Operator +</b>:<br>
<table border=1>
<tr><td>&nbsp;</td><td>int</td><td>string</td><td>array</td></tr>
<tr><td>int</td><td>int</td><td>int</td><td>N/A</td></tr>
<tr><td>string</td><td>string</td><td>string</td><td>string</td>
<tr><td>array</td><td>array</td><td>array</td><td>array</td>
</table><br>
Value is added to int and appended to string and array.<br>
Operator + applied to structure will be applied to all elements.<br>

<b>Operatpr -</b>:<br>
<table border=1>
<tr><td>&nbsp;</td><td>int</td><td>string</td><td>array</td></tr>
<tr><td>int</td><td>int</td><td>int</td><td>N/A</td></tr>
<tr><td>string</td><td>string slice</td><td>string segment(str as int)</td><td>N/A</td></tr>
<tr><td>array</td><td>array segment</td><td>array segment(str as int)</td><td>N/A</td></tr>
<tr><td>segment</td><td>segment</td><td>segment(str as int)</td><td>N/A</td></tr>
<tr><td>slice</td><td>slice</td><td>slice(str as int)</td><td>N/A</td></tr>
</table>
Example:<br>
<xmp>
s="hello world"
t=s-5
</xmp>
Value of t is a string segment corresponding to "world".<br>
Operator - applied to structure will be applied to all elements.<br>
"str as int" means that right hand side string value converted to int value.<br>

<b>Operator *</b>:<br>
<table border=1>
<tr><td>&nbsp;</td><td>int</td><td>string</td><td>array</td></tr>
<tr><td>int</td><td>int</td><td>int</td><td>N/A</td></tr>
<tr><td>string</td><td>string</td><td>N/A</td><td>N/A</td></tr>
<tr><td>array</td><td>array</td><td>N/A</td><td>N/A</td></tr>
</table><br>
Operator * will multiply two ints and duplicate strings and arrays.<br>

<b>Operator /</b>:<br>
<table border=1>
<tr><td>&nbsp;</td><td>int</td><td>string</td><td>array</td></tr>
<tr><td>int</td><td>int</td><td>int</td><td>N/A</td></tr>
<tr><td>string</td><td>string segment</td><td>string segment(str as int)</td><td>N/A</td></tr>
<tr><td>array</td><td>array segment</td><td>array segment(str as int)</td><td>N/A</td></tr>
</table><br>
Example:<br>
<xmp>
s="hello world"
t=s/5
</xmp>
Value of t is a string segment that corresponds to "hello".<br>
<b>Operator %</b>:<br>
<table border=1>
<tr><td>&nbsp;</td><td>int</td><td>string</td><td>array</td></tr>
<tr><td>int</td><td>int</td><td>int(str as int)</td><td>N/A</td></tr>
<tr><td>string</td><td>N/A</td><td>N/A</td><td>string slice</td></tr>
<tr><td>array</td><td>N/A</td><td>N/A</td><td>array slice</td></tr>
</table><br>
Example:<br>
<xmp>
s="hello world"
t=s%{0,2,4}
</xmp>
Value of t corresponds to letters "h", first "l" and "o".
Operator % applied to a structure will be applied to all of it's elements.<br>

<b>Operator #</b>:<br>
Operator # is unrary and can only be applied to strings and arrays.
Result of this operator is integer value equal to length of a string or number of elements of an array.<br>

<b>Operator ~</b><br>
Operator ~ is can be both unary or binary. In unary form it is supposed that left side is 0.
Operator ~ produces random integer value in range [left side,right side). Right side should be integer.<br>
There are "self modify" versions of all operators:<br>
+=,-=,/=,*=,%=<br>


<h3 id="expstat">5. Expression related statements.</h3>

Assignment operation "=" is a statement, not an expression. Following source will compile, but have different meaning in dense script than in C:<br>
<xmp>
a=b=c
</xmp>
Those are two statements:<br>
<code>
a=b<br>
=c
</code>
"=c" will try to assign to a last stand alone expression evaluated (if any).
If variable is uninitialized, it will have type of expression on the right side on an assignment operation.
But assignment operation cannot change type of already initialized variable. For example:<br>
<xmp>
a={1,2,3}
a=2
</xmp>
Value of a is still array of 3 elements.<br>
Overwrite assignment operation "==" will try to change type of variable on the right side, if possible.<br>

Stand alone expression is evaluated and stored, and can be used in a delayed assignment.
See example in conditional statement description.<br>

<h3 id="ctrlflow">6. Control flow statements</h3>
<b>Conditional statement</b><br>
Typical if/then/else in dense script looks like this:<br>
<b>?</b><i>condition</i><b>`</b><i>then statements</i><b>:</b><i>else statements</i><b>\</b><br>
: with <i>else statements</i> are optional.<br>
In a boolean context of <i>condition</i> following operators are supported:<br>
&amp; - boolean and<br>
| - boolean or<br>
unary ! is boolean negation<br<
binary ! is "not equal"<br>
= is "equal to"<br>
< is "less than"<br>
<= is "less than or equal to"<br>
> is "greater than"<br>
>= is "greater than or equal to"<br>
There is expression version of conditional statement:<br>
<b>?</b><i>condition</i><b>`</b><i>then expression</i><b>:</b><i>else expression</i><b>\</b><br>
Only one expression can be <i>then expression</i> and <i>else expression</i>.
<i>else expression</i> is not optional in this case.<br>
Example:<br>
<xmp>
a=?x>0`1:?x`0:-1\\
</xmp>
In this example a is equal to sign of x.<br>
Single expression can be used as condition. In this case for integer true is non-zero value, false is zero.
For string and array true is non-empty string or array and false otherwise.
There is one more possible use of confitional statement, in pair with delayed assignment.
<xmp>
?x>y`x:y\=a
</xmp>
If x is greater than y, than value of a assigned to x, and to y otherwise.
<b>Select/switch statement</b><br>
<b>??</b><i>expression</i><b>`</b><i>value1</i><b>:</b><i>statements1</i><b>`</b><i>value2</i><b>:</b><i>statements2</i><b>`:</b><i>default case</i><b>\</b><br>
<b>Loops</b><br>
Typical for(int i=0;i&lt;n;i++) in a dense script looks like this:<br>
<b>@</b><i>loopvar</i><b>`</b><i>start value</i><b>&lt;</b><i>end value</i>&nbsp;<i>statements</i><b>\</b><br>
Inclusive loop:<br>
<b>@</b><i>loopvar</i><b>`</b><i>start value</i><b>:</b><i>end value</i>&nbsp;<i>statements</i><b>\</b><br>
Multidimentional loop:<br>
<b>@</b><i>loopvar1loopvar2loopvar3</i><b>`</b><i>start value1,start value2,start value3</i><b>:</b><i>end value1,end value2,end value3</i>&nbsp;<i>statements</i><b>\</b><br>
Infinite loop:
<b>@:</b><i>statements</i><b>\</b><br>
Anonymous:<br>
<b>@`</b><i>start value</i><b>:</b><i>end value</i>&nbsp;<i>statements</i><b>\</b><br>
Array iteration loop<br>
<b>@{</b><i>var</i><b>:</b><i>array</i><b>}</b><i>statements</i><b>\</b><br>
<b>@&lt;</b> - next iteration (aka continue)<br>
<b>@&gt;</b> - break loop<br>
Examples:<br>
<xmp>
@i`0:9&lt;i\
</xmp>
This will print numers from 0 to 9.<br>
<h3 id="func">7. Functions and methods.</h3><br>
Function definition looks like this:<br>
<b>#</b><i>function name</i><b>(</b><i>arg1</i><i>arg2</i><i>arg3</i><b>)</b><i>function body</i><b>\</b><br>
If there are no arguments, braces can be omited. If &amp; is preceding argument variable, value is passed "by reference".
Value of variable can be modified this way, but overwrite assignment won't work.<br>
Function call looks naturally:<br>
<i>function name</i><b>(</b><i>arg1</i>,<i>arg2</i><b>)</b><br>
Return statement starts with exclamation symbol. If followed by expression, value is returned.<br>
For example:<br>
<xmp>
#f(x)!x*x\
x=f(5)
<<x
</xmp>
Output is "25".<br>
Method is almost like function, but have "this" value, and have slightly different call syntax.
Method definition looks like this:<br>
<b>#?</b><i>method name</i><b>(</b><i>arg1</i><i>arg2</i><i>arg3</i><b>)</b><i>function body</i><b>\</b><br>
This value is dot. It can be modified. Method call looks like this:<br>
<i>var name</i><i>method name</i><b>(</b><i>expr1</i><b>,</b><i>expr2</i><b>)</b><br>
Example:<br>
<xmp>
#?m(x).+=x\
a=1
am(2)
<<a
</xmp>
Output: 3<br>
Local variables are created with struct slice constructor with empty left operand.<br>
Example:<br>
<xmp>
#f(xy)
'a=x+y/2
!x/a+(y/a)
\
<<f(4,8)
</xmp>
Output is: 1<br>
Not that x+y/2 is actualy (x+y)/2. And x/a+y/a is ((x/a)+y)/a. Beware.

<h3 id="io">8. Input and output</h3>
There are standard and curses modes of output. Curses is default. Command line switch can be used to switch to turn on standard mode.<br>
There are three output operations:<br>
&lt;<i>expression</i> - print value without end of line<br>
&lt;&lt;<i>expression</i> - print value with end of line and refresh/flush curses terminal.<br>
&lt;!<i>expression</i> - print value to stderr stream. Can be redirected from command line.<br>
There are two input operations:<br>
&gt;<i>variable</i> - input one symbol. Integer code of symbol is stored in a variable. In standard mode wont return until enter is pressed.<br>
&gt;&gt;<i>variable</i> - input one line.<br>
Curses only operations:<br>
^<i>x</i>,<i>y</i> - set cursor position<br>
$<i>text color</i>,<i>background color</i><br>
Comma and background color is optional.<br>
It is also possible to change color of text and background in interpolated strings.<br>
\c<i>N</i> - to change text color to <i>N</i>. N is number, not expression.<br>
\b<i>N</i> - to change background color to <i>N</i>.<br>
\r - return color after previous \c or \b<br>
\R - equivalent to \r\r. Can be used to return color after \cN\bN.<br>
<h3 id="examples">9. Examples</h3>
Here are some examples.
<xmp>
s="hello world"
s-5="test"
<<s
</xmp>
Output is "hello test"
<xmp>
a'xy={1,2}
b'xy={3,2}
a+=b
<<"ax+ay=$(ax+ay)"
</xmp>
Output is "ax+ay=8"<br>
<xmp>
#f(a,b)'xy={a,b}!'xy\
a'xy=f(2,3)
<<a
</xmp>
It is possible to return slice of local scope as struct slice.<br>
</body>
</html>
